import Vue from 'vue'
import VueRouter from 'vue-router'
import ghRouter from './module/ghRouter'
import cjjRouter from './module/cjjRouter'
import txqRouter from './module/txqRouter'
Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
  routes: [...ghRouter, ...cjjRouter, ...txqRouter]
})

router.beforeEach((to, from, next) => {
  // 要访问的路径
  if (to.path === '/User' || to.path === '/Register') return next()
  // 获取token
  const tokenStr = localStorage.getItem('token')
  // 如果没有携带token就直接到login页面
  if (!tokenStr) return next('/User' || '/Register')
  next()
})
export default router
